package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.dao.domain.PayRechargePlan;
import com.yunkeji.dao.mapper.PayRechargePlanMapper;
import com.yunkeji.dao.vo.PayRechargePlanVo;
import com.yunkeji.system.domain.bo.PayRechargePlanBo;
import com.yunkeji.system.service.IPayRechargePlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 充值方案Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class PayRechargePlanServiceImpl implements IPayRechargePlanService {

    private final PayRechargePlanMapper baseMapper;

    /** 查询充值方案 */
    @Override
    public PayRechargePlanVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询充值方案列表 */
    @Override
    public TableDataInfo<PayRechargePlanVo> queryPageList(
            PayRechargePlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayRechargePlan> lqw = buildQueryWrapper(bo);
        Page<PayRechargePlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询充值方案列表 */
    @Override
    public List<PayRechargePlanVo> queryList(PayRechargePlanBo bo) {
        LambdaQueryWrapper<PayRechargePlan> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(PayRechargePlan::getGold);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayRechargePlan> buildQueryWrapper(PayRechargePlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayRechargePlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCnyPrice() != null, PayRechargePlan::getCnyPrice, bo.getCnyPrice());
        lqw.eq(bo.getGold() != null, PayRechargePlan::getGold, bo.getGold());
        lqw.eq(bo.getPayType() != null, PayRechargePlan::getPayType, bo.getPayType());
        return lqw;
    }

    /** 新增充值方案 */
    @Override
    public Boolean insertByBo(PayRechargePlanBo bo) {
        PayRechargePlan add = BeanUtil.toBean(bo, PayRechargePlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改充值方案 */
    @Override
    public Boolean updateByBo(PayRechargePlanBo bo) {
        PayRechargePlan update = BeanUtil.toBean(bo, PayRechargePlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(PayRechargePlan entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除充值方案 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
